package com.foo.web.controller.api;

import java.sql.SQLException;
import java.util.List;

import com.foo.common.enums.OperatorType;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.foo.common.annotation.Log;
import com.foo.common.enums.BusinessType;
import com.foo.wechat.domain.WxCanteenMenu;
import com.foo.wechat.service.IWxCanteenMenuService;
import com.foo.common.core.controller.BaseController;
import com.foo.common.core.domain.AjaxResult;

/**
 * 餐厅菜单Api
 * 
 * @author foo
 * @date 2025-11-11
 */
@Controller
@RequestMapping("/wechat/api/WxCanteenMenu")
public class WxCanteenMenuApi extends BaseController
{
    @Autowired
    private IWxCanteenMenuService wxCanteenMenuService;

    /**
     * 查询餐厅菜单列表
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(WxCanteenMenu wxCanteenMenu,Integer pageNum, Integer pageSize)
    {
        if(pageNum != null){
            Integer size = 10;
            if(pageSize != null){
                size = pageSize;
            }
            PageHelper.startPage(pageNum, size);
        }
        List<WxCanteenMenu> list = wxCanteenMenuService.selectWxCanteenMenuList(wxCanteenMenu);
        return AjaxResult.success(list);
    }

    /**
     * 查询餐厅菜单详情
     */
    @GetMapping("/detail")
    @ResponseBody
    @Log(title = "餐厅菜单", businessType = BusinessType.Detail, operatorType = OperatorType.MOBILE)
    public AjaxResult detail(Long menuId)
    {
        return AjaxResult.success(wxCanteenMenuService.selectWxCanteenMenuById(menuId));
    }

    /**
     * 新增餐厅菜单
     */
    @PostMapping("/add")
    @ResponseBody
    @Log(title = "餐厅菜单", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addSave(WxCanteenMenu wxCanteenMenu) throws Exception
    {
        if(wxCanteenMenuService.insertWxCanteenMenu(wxCanteenMenu) > 0){
            List<WxCanteenMenu> wxCanteenMenuList = wxCanteenMenuService.selectWxCanteenMenuList(wxCanteenMenu);
            if(wxCanteenMenuList.size() > 0){
                return AjaxResult.success(wxCanteenMenuList.get(0));
            }else{
                //数据添加成功之后查询不到数据 回滚
                throw new SQLException("数据为空");
            }
        }
        return toAjax(0);
    }

    /**
     * 修改餐厅菜单
     */
    @PostMapping("/edit")
    @ResponseBody
    @Log(title = "餐厅菜单", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editSave(WxCanteenMenu wxCanteenMenu)
    {
        return toAjax(wxCanteenMenuService.updateWxCanteenMenu(wxCanteenMenu));
    }

    /**
     * 删除餐厅菜单(单个)
     */
    @PostMapping( "/remove")
    @ResponseBody
    @Log(title = "餐厅菜单", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(Long id)
    {
        return toAjax(wxCanteenMenuService.deleteWxCanteenMenuById(id));
    }
    
}
